package com.zjz.studynotes;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.ComponentActivity;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatDialog;
import androidx.recyclerview.widget.RecyclerView;

import com.zjz.studynotes.bean.StudyNoteInfo;
import com.zjz.studynotes.db.StudyNotesDb;

import java.util.ArrayList;
import java.util.List;

/**
 * Description: 编辑，新增笔记页面
 * Author: zjz
 * Date:2025/5/5 13:06
 */
public class EditActivity extends ComponentActivity {
    private EditText mEtTitle;
    private EditText mEtContent;
    private LinearLayout mLyEvaluate;
    private Button mBtnAdd;
    private Button mBtnCancel;

    private Button mBtnEvaluate;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit);


        int infoID = getIntent().getIntExtra("info", -1);

        mEtTitle = findViewById(R.id.et_title);
        mEtContent = findViewById(R.id.et_content);
        mBtnAdd = findViewById(R.id.btn_save);
        mBtnCancel = findViewById(R.id.btn_cancel);
        mLyEvaluate = findViewById(R.id.ly_evaluate);
        mBtnEvaluate = findViewById(R.id.btn_evaluate);

        mBtnCancel.setOnClickListener(view -> finish());
        mBtnAdd.setOnClickListener(view -> save(infoID));
        mBtnEvaluate.setOnClickListener(v -> showEvaluateDialog(infoID));

        initData(infoID);
    }


    private void initData(int infoID) {
        StudyNoteInfo info = StudyNotesDb.getInstance(this).getStudyNoteInfoDao().queryInfoByID(infoID);
        if (info != null) {
            mEtTitle.setText(info.getTitle());
            mEtContent.setText(info.getContent());
            mLyEvaluate.removeAllViews();
            List<String> list = info.getEvaluate();
            if (list != null && list.size() > 0) {
                for (int i = 0; i < list.size();i++) {
                    TextView tv = new TextView(this);
                    tv.setText(new StringBuffer().append(i+1).append(".").append(list.get(i)).toString());
                    tv.setTextSize(20);
                    mLyEvaluate.addView(tv);
                }
            }


        }
    }


    /**
     * 保存校验笔记数据，存入数据库
     * @param infoID
     */
    private void save(int infoID) {
        if (TextUtils.isEmpty(mEtTitle.getText().toString())) {
            Toast.makeText(this, "请输入标题", Toast.LENGTH_SHORT).show();
            return;
        }

        if (TextUtils.isEmpty(mEtContent.getText().toString())) {
            Toast.makeText(this, "请输入内容", Toast.LENGTH_SHORT).show();
            return;
        }


        StudyNoteInfo info = StudyNotesDb.getInstance(this).getStudyNoteInfoDao().queryInfoByID(infoID);


        if (info == null) {
            info = new StudyNoteInfo();
        }

        info.setTitle(mEtTitle.getText().toString());
        info.setContent(mEtContent.getText().toString());
        info.setEvaluate(info.getEvaluate());

        StudyNotesDb.getInstance(this).getStudyNoteInfoDao().insert(info);
        Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
        finish();
    }


    private void showEvaluateDialog(int infoID) {
        AppCompatDialog dialog = new AppCompatDialog(this); // 使用当前Activity的上下文创建AppCompatDialog对象。
        dialog.setContentView(R.layout.dialog_add_evaluate); // 设置自定义布局。
        dialog.setCancelable(false); // 设置是否可以通过点击返回键或点击外部来取消对话框。
        dialog.show(); // 显示对话框。

        EditText et = dialog.findViewById(R.id.et_evaluate);
        Button btnSave = dialog.findViewById(R.id.btn_save);
        Button btnCancel = dialog.findViewById(R.id.btn_cancel);

        btnCancel.setOnClickListener(v -> dialog.dismiss());

        btnSave.setOnClickListener(v -> {
            if (TextUtils.isEmpty(et.getText().toString())) {
                Toast.makeText(EditActivity.this, "请输入评价",Toast.LENGTH_SHORT).show();
            } else {
                addEvaluate(infoID, et.getText().toString());
                dialog.dismiss();
            }

        });
    }

    private void addEvaluate(int infoID, String eva) {
        StudyNoteInfo info = StudyNotesDb.getInstance(this).getStudyNoteInfoDao().queryInfoByID(infoID);
        List<String> evaList = info.getEvaluate();
        if (evaList == null) {
            evaList = new ArrayList<>();
        }

        evaList.add(eva);
        info.setEvaluate(evaList);
        StudyNotesDb.getInstance(this).getStudyNoteInfoDao().insert(info);
        Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();

        initData(infoID);
    }
}
